<?php
	//upload pdf file
	function uploadFilePdf($path,$control_name)
	{
		if(($_FILES[$control_name]["type"] == "application/pdf") && ($_FILES[$control_name]["size"] <= 45937784))
 
		//if($_FILES[$control_name]["type"] == "application/pdf")
			{
				if($_FILES[$control_name]["error"] > 0)
				{
					errorMessage("Failed while uploaded file. 
								  Error Number: ".$_FILES[$control_name]["error"]);	
				}
				else
				{
					$file_name = $_FILES[$control_name]["name"];
					if(file_exists($path."/" . $file_name))	
					{
						errorMessage("File: " . $file_name . " already exists.");
					}
					else
					{
						if(!move_uploaded_file($_FILES[$control_name]["tmp_name"], 
						   $path."/".$file_name))	
						{
							errorMessage("Failed while uploaded file to server");	   
						}
					}
				}
			}			
			else
					errorMessage("Invalid File!");
	}
	
	//upload image file
	function uploadFile($path,$control_name)
	{
		if((($_FILES[$control_name]["type"] == "image/png")
			|| ($_FILES[$control_name]["type"] == "image/jpeg")
			|| ($_FILES[$control_name]["type"] == "image/pjpeg")
			|| ($_FILES[$control_name]["type"] == "image/gif"))
			&& ($_FILES[$control_name]["size"] <= 2000000))
			{
				if($_FILES[$control_name]["error"] > 0)
				{
					errorMessage("Failed while uploaded file. 
								  Error Number: ".$_FILES[$control_name]["error"]);	
				}
				else
				{
					$file_name = $_FILES[$control_name]["name"];
					if(file_exists($path."/" . $file_name))	
					{
						errorMessage("File: " . $file_name . " already exists.");
					}
					else
					{
						if(!move_uploaded_file($_FILES[$control_name]["tmp_name"], 
						   $path."/".$file_name))	
						{
							errorMessage("Failed while uploaded file to server");	   
						}
						
					}
				}
				
			}			
			else
					errorMessage("Invalid File!");
	}
	
// this function to crop image and resize
function validImage($file,$imgX, $imgY){
	if (isset($_FILES[$file]) && !$_FILES[$file]["error"]){	
		$imgSize = getimagesize($_FILES[$file]["tmp_name"]);
		if (($_FILES[$file]["type"] == "image/jpeg") || 
			($_FILES[$file]["type"] == "image/pjpeg")){		
			if ($imgSize[1] > $imgY){//check hight 
					return "Invalid file size Height : Maximum Height is :".$imgY;
			}elseif ($imgSize[0] > $imgX){
					return "Invalid file size Width :  Maximum Width is :".$imgX;	
			}		
		}
		else{
			return "Invalid file type";
		}
		return true;
	}else{return "Please upload the photo";}
}

function createImageAndThumb($src_file,$image_name,
							$max_width,
						    $max_height,
							$tag,
							$path,
							$orig_name)
{
	//global $mosConfig_absolute_path;
	
	$types = array( 
        IMAGETYPE_JPEG => 'jpeg', 
        IMAGETYPE_GIF => 'gif', 
        IMAGETYPE_PNG => 'png' 
    ); 
	
    ini_set('memory_limit', '120M');

	
	$src_file = urldecode($src_file);
	/*if (extension_loaded('exif')) 
	{
		$type2 = exif_imagetype($src_file);
		$types = array( 
			IMAGETYPE_JPEG => 'jpeg', 
			IMAGETYPE_GIF => 'gif', 
			IMAGETYPE_PNG => 'png' 
		); 
    
		$type = $types[$type2]; 
	}
	else
	{*/
		$orig_name = strtolower($orig_name);
		$findme  = '.jpg';
		$pos = strpos($orig_name, $findme);
		if ($pos === false)
		{
			$findme  = '.jpeg';
			$pos = strpos($orig_name, $findme);
			if ($pos === false)
			{
				$findme  = '.gif';
				$pos = strpos($orig_name, $findme);
				if ($pos === false)
				{
					$findme  = '.png';
					$pos = strpos($orig_name, $findme);
					if ($pos === false)
					{
						return;
					}
					else
					{
						$type = "png";
					}
				}
				else
				{
					$type = "gif";
				}
			}
			else
			{
				$type = "jpeg";
			}
		}
		else
		{
			$type = "jpeg";
		}
	//}
	
/*	$max_h = $max_height;
	$max_w = $max_width;
	$max_thumb_h = $max_height_t;
	$max_thumb_w = $max_width_t;*/
	
	if ( file_exists( "$path/$image_name")) {
		
		errorMessage("File: " . $image_name . " already exists.");
		unlink( "$path/$image_name");
	}
	
	$read = 'imagecreatefrom' . $type; 
	$write = 'image' . $type; 
	
	$source_gdim = $read($src_file);
	
	// height/width
	list( $source_width, $source_height) = getimagesize( $src_file );
/*	
	$imginfo = getimagesize($src_file);
	$$source_width = $imginfo[0];
	$source_height = $imginfo[1];

	$zoom_h = $max_h / $src_h;
    $zoom_w = $max_w / $src_w;
    $zoom   = min($zoom_h, $zoom_w);
    $dst_h  = $zoom<1 ? round($src_h*$zoom) : $src_h;
    $dst_w  = $zoom<1 ? round($src_w*$zoom) : $src_w;
*/

  $source_aspect_ratio = $source_width / $source_height;
  $desired_aspect_ratio = $max_width / $max_height;

  if ( $source_aspect_ratio > $desired_aspect_ratio )
  {
    //
    // Triggered when source image is wider
    //
    $temp_height = $max_height;
    $temp_width = ( int ) ( $max_height * $source_aspect_ratio );
  }
  else
  {
    //
    // Triggered otherwise (i.e. source image is similar or taller)
    //
    $temp_width = $max_width;
    $temp_height = ( int ) ( $max_width / $source_aspect_ratio );
  }



  //
  // Resize the image into a temporary GD image
  //

  $temp_gdim = imagecreatetruecolor( $temp_width, $temp_height );
  imagecopyresampled(
    $temp_gdim,
    $source_gdim,
    0, 0,
    0, 0,
    $temp_width, $temp_height,
    $source_width, $source_height
  );
  $x0 = ( $temp_width - $max_width ) / 2;
  $y0 = ( $temp_height - $max_height ) / 2;

  $desired_gdim = imagecreatetruecolor( $max_width, $max_height );
  
  imagecopy(
    $desired_gdim,
    $temp_gdim,
    0, 0,
    $x0, $y0,
	$max_width, $max_height
  );

/*	$dst_img = imagecreatetruecolor($dst_w,$dst_h);	
	$white = imagecolorallocate($dst_img,255,255,255);
	imagefill($dst_img,0,0,$white);
	imagecopyresampled($dst_img,$src_img, 0,0,0,0, $dst_w,$dst_h,$src_w,$src_h);
	$textcolor = imagecolorallocate($dst_img, 255, 255, 255);
	if (isset($tag))
		imagestring($dst_img, 5, 5, 5, "$tag", $textcolor);  */
		
	if($type == 'jpeg'){
        $desc_img = $write($desired_gdim,"$path/$image_name", 75);
	}else{
        $desc_img = $write($desired_gdim,"$path/$image_name", 2);
	}
	
}

function createResizeImage($src_file,$image_name,
							$max_width,
						    $max_height,
							$tag,
							$path,
							$orig_name)
{
	//global $mosConfig_absolute_path;
	
	$types = array( 
        IMAGETYPE_JPEG => 'jpeg', 
        IMAGETYPE_GIF => 'gif', 
        IMAGETYPE_PNG => 'png' 
    ); 
	
    ini_set('memory_limit', '32M');

	
	$src_file = urldecode($src_file);
	/*if (extension_loaded('exif')) 
	{
		$type2 = exif_imagetype($src_file);
		$types = array( 
			IMAGETYPE_JPEG => 'jpeg', 
			IMAGETYPE_GIF => 'gif', 
			IMAGETYPE_PNG => 'png' 
		); 
    
		$type = $types[$type2]; 
	}
	else
	{*/
		$orig_name = strtolower($orig_name);
		$findme  = '.jpg';
		$pos = strpos($orig_name, $findme);
		if ($pos === false)
		{
			$findme  = '.jpeg';
			$pos = strpos($orig_name, $findme);
			if ($pos === false)
			{
				$findme  = '.gif';
				$pos = strpos($orig_name, $findme);
				if ($pos === false)
				{
					$findme  = '.png';
					$pos = strpos($orig_name, $findme);
					if ($pos === false)
					{
						return;
					}
					else
					{
						$type = "png";
					}
				}
				else
				{
					$type = "gif";
				}
			}
			else
			{
				$type = "jpeg";
			}
		}
		else
		{
			$type = "jpeg";
		}
	//}
	
	$max_h = $max_height;
	$max_w = $max_width;
	
	if ( file_exists( "$path/$image_name")) {
		
		//errorMessage("File: " . $image_name . " already exists.");
		unlink( "$path/$image_name");
	}
	
	
	$read = 'imagecreatefrom' . $type; 
	$write = 'image' . $type; 
	
	$src_img = $read($src_file);
	
	// height/width
	$imginfo = getimagesize($src_file);
	$src_w = $imginfo[0];
	$src_h = $imginfo[1];
	
	$zoom_h = $max_h / $src_h;
    $zoom_w = $max_w / $src_w;
    $zoom   = min($zoom_h, $zoom_w);
    $dst_h  = $zoom<1 ? round($src_h*$zoom) : $src_h;
    $dst_w  = $zoom<1 ? round($src_w*$zoom) : $src_w;
	$dst_img = imagecreatetruecolor($dst_w,$dst_h);
	
	$white = imagecolorallocate($dst_img,255,255,255);
	imagefill($dst_img,0,0,$white);
	imagecopyresampled($dst_img,$src_img, 0,0,0,0, $dst_w,$dst_h,$src_w,$src_h);
	$textcolor = imagecolorallocate($dst_img, 255, 255, 255);
	if (isset($tag))
		imagestring($dst_img, 5, 5, 5, "$tag", $textcolor);  
	if($type == 'jpeg'){
        $desc_img = $write($dst_img,"$path/$image_name", 75);
	}else{
        $desc_img = $write($dst_img,"$path/$image_name", 2);
	}
}
	function deleteFile($path)
	{
		if(file_exists($path))
			@unlink($path);
	}
?>